package com.yaoandw.htmlparse;

import java.net.URL;

import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.beans.HTMLLinkBean;
import org.htmlparser.beans.HTMLTextBean;
import org.htmlparser.beans.LinkBean;
import org.htmlparser.beans.StringBean;
import org.htmlparser.util.ParserException;
import org.htmlparser.visitors.NodeVisitor;

public class ParseTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String url = "http://www.actiz.com:8080/bbs/";
//		visit(url);
//		text(url);
//		string(url);
		link(url);
		System.out.println("==================");
		htmllink(url);
	}
	private static void visit(String url){
		try {
		    Parser parser = new Parser();
		    parser.setURL(url);
		    parser.setEncoding(parser.getEncoding());
		    NodeVisitor visitor = new NodeVisitor() {
		        public void visitTag(Tag tag) {
		        	System.out.println("testVisitorAll()  Tag name is :"
		                    + tag.getTagName() + " \n Class is :"
		                    + tag.getClass());
		        }
		    };
		    parser.visitAllNodesWith(visitor);
		} catch (ParserException e) {
		    e.printStackTrace();
		}
	}
	//Extract links from a URL.
	private static void link(String url){
		Parser parser = new Parser();
		LinkBean linkBean = new LinkBean();
		linkBean.setURL(url);
		URL[] urls = linkBean.getLinks();
		for (int i = 0; i < urls.length; i++) {
		    URL url1 = urls[i];
		    System.out.println("testLinkBean() -url  is :" + url1);
		}
	}
	//Display the links from a URL.
	private static void htmllink(String url){
		Parser parser = new Parser();
		HTMLLinkBean linkBean = new HTMLLinkBean();
		linkBean.setURL(url);
		URL[] urls = linkBean.getLinks();
		for (int i = 0; i < urls.length; i++) {
			URL url1 = urls[i];
			System.out.println("testLinkBean() -url  is :" + url1);
		}
	}
	//Display the textual URL contents.
	private static void text(String url){
		Parser parser = new Parser();
		HTMLTextBean htmlTextBean = new HTMLTextBean();
		htmlTextBean.setURL(url);
		System.out.println(htmlTextBean.getText());
	}
	//Extract strings from a URL.
//	Text within <SCRIPT></SCRIPT> tags is removed.
//	The text within <PRE></PRE> tags is not altered.
	private static void string(String url){
		StringBean stringBean = new StringBean();
		stringBean.setURL(url);
		System.out.println(stringBean.getStrings());
	}
}
